package com.office.anywher.views;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.office.anywher.R;
import com.office.anywher.beans.org.TreeNode;
import com.office.anywher.org.ICallBack;
import com.wenxy.common.IConst;

public class TreeView {
	private Context mContext;
	private  List<List<TreeNode>> mOrgCollectList;
	private ICallBack mCallBack;

	public TreeView(Context context,
			 List<List<TreeNode>> orgCollectList,ICallBack callBack) {
		mContext = context;
		mOrgCollectList = orgCollectList;
		mCallBack = callBack;
	}

	public View getTreeView() {
		LayoutInflater aInflater = LayoutInflater.from(mContext);
		LinearLayout linear = (LinearLayout) aInflater.inflate(R.layout.tree_view,null);
		for(List<TreeNode> list:mOrgCollectList){
			for(TreeNode node:list){
				linear.addView(getItemView(node));
			}
		}
		return linear;
	}
	
	public View getItemView(final TreeNode node) {
		LayoutInflater aInflater = LayoutInflater.from(mContext);
		RelativeLayout item = (RelativeLayout)aInflater.inflate(R.layout.tree_item_view, null);
		item.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
		CheckBox checkBox = (CheckBox)item.findViewById(R.id.tree_checkBox);
		TextView textView = (TextView)item.findViewById(R.id.tree_textview);
		ImageView imageView = (ImageView)item.findViewById(R.id.expand_log);
		textView.setText(node.organName);//+",id:"+node.id+",pid:"+node.parentOrganId
		item.setId(node.id);
		item.setTag(node);
		checkBox.setTag(node);
		if(node.organType != IConst.Http.Org.ROOT_ORGAN_TYPE){
			item.removeView(imageView);
			if(node.organType == IConst.Http.Org.FINAL_ORGAN_TYPE){
				item.setBackgroundColor(Color.parseColor("#FFFAFA"));
			}else{
				item.setBackgroundColor(Color.parseColor("#BEBEBE"));
			}
			item.setVisibility(View.GONE);
		}else{
			textView.setTextColor(Color.WHITE);
			textView.setTextSize(18);
			item.setBackgroundColor(Color.parseColor("#45474a"));
		}
		item.setOnClickListener(new OnClickListener(){
			private boolean show = node.organType == IConst.Http.Org.ROOT_ORGAN_TYPE?false:true;
			@Override
			public void onClick(View view) {
				show = !show;
				mCallBack.execute(view,show);
			}
		});
		checkBox.setOnClickListener(new OnClickListener(){
			private boolean show = node.organType == IConst.Http.Org.ROOT_ORGAN_TYPE?false:true;
			@Override
			public void onClick(View view) {
				show = !show;
				mCallBack.execute(view,show);
			}
		});
		return item;
	}
}
